package com.bwda.dsrs.filecenter.service.impl;


import com.bwda.dsrs.base.centerbase.util.PageHelperUtil;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.terminalblacklist.TerminalBlackListCondition;
import com.bwda.dsrs.filecenter.domain.condition.terminaluser.TerminalUserCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.po.terminal.TerminalPo;
import com.bwda.dsrs.filecenter.domain.condition.terminal.TerminalCondition;
import com.bwda.dsrs.filecenter.domain.po.terminalblacklist.TerminalBlackListPo;
import com.bwda.dsrs.filecenter.domain.po.terminaluser.TerminalUserPo;
import com.bwda.dsrs.filecenter.mapper.dsrs.TerminalMapper;
import com.bwda.dsrs.filecenter.mapper.dsrs.TerminalUserMapper;
import com.bwda.dsrs.filecenter.service.TerminalBlackListService;
import com.bwda.dsrs.filecenter.service.TerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yangjun
 */
@Service
public class TerminalServiceImpl extends BaseServiceImpl<TerminalPo, TerminalCondition, TerminalMapper> implements TerminalService {


	@Autowired
	private TerminalMapper terminalMapper;

	@Autowired
	private TerminalUserMapper terminalUserMapper;

	@Autowired
	private TerminalBlackListService terminalBlackListService;

	@Override
	public List<TerminalPo> selectTlist(TerminalCondition condition) {
		PageHelperUtil.startPage(condition.getPageNum(),condition.getPageSize());
		List<TerminalPo> terminalPoListpo =terminalMapper.selectList(condition);
		List<TerminalUserPo>  terminalUserPoList   =terminalUserMapper.selectList(new TerminalUserCondition());
		//加入使用人列表
		for (int i= 0;i<terminalPoListpo.size();i++){
			String userList = "";
			for (int j= 0;j<terminalUserPoList.size();j++){
				if (terminalPoListpo.get(i).getTerminalCode().equals(terminalUserPoList.get(j).getTerminalNo())){
					userList+=terminalUserPoList.get(j).getUserName()+",";
				}
			}
			if (!"".equals(userList)){
				userList=userList.substring(0,userList.length()-1);
			}
			terminalPoListpo.get(i).setUserList(userList);
		}
		return terminalPoListpo;
	}

	@Override
	public void isJurisdiction(TerminalPo po) throws BusinessServiceException {
		//判断终端状态
		if(po.getTerminalState()== BusinessConstants.NumberConstant.ONE){
			throw  new BusinessServiceException("终端被冻结");
		}
		//判断ip黑名单
		TerminalBlackListCondition condition = new TerminalBlackListCondition();
		condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
		List<TerminalBlackListPo> list =	terminalBlackListService.queryList(condition);
		String[] ips = po.getIp();
		for (int j = 0;j<ips.length;j++){
			for (int i = 0;i<list.size();i++){
				if (terminalBlackListService.ipExistsInRange(po.getIp()[j],list.get(i).getIpStart(),list.get(i).getIpEnd())){
					throw  new BusinessServiceException("ip被禁用");
				}
				if (list.get(i).getMacAddr()!=null){
					if (po.getMacAddr().equals(list.get(i).getMacAddr())){
						throw  new BusinessServiceException("Mac地址被禁用");
					}
				}
			}
		}
	}

	@Override
	public Integer getNextVal() {
		return this.getMapper().getNextVal();
	}

	@Override
	public TerminalPo selectByMac(String mac) {
		return this.getMapper().selectByMac(mac);
	}

	@Override
	public TerminalPo selectByNo(String no) {
		return this.getMapper().selectByNo(no);
	}

}